package kd.xkqj.unittest.plugin.setting;


import javax.swing.*;

import java.awt.*;

/**
 * @description: 插件设置UI
 * @author feng_jun_peng
 * @create 2024-12-18 09:18
 * @version 1.0
 */
public class PluginSettingsUI {
    private JPanel mainPanel;

    //region 封装字段
    private JTextArea importPackagesTextArea; //新增方法时，默认依赖的包
    private JCheckBox showConfirmCheckBox; //每次创建或更新时，弹窗确认

    private JCheckBox genPublicCheckBox; //生成公共方法
    private JCheckBox genProtectedCheckBox; //生成保护方法
    private JCheckBox genPrivateCheckBox; //生成私有方法
    private JCheckBox addUnsupportedExCheckBox; // 新增方式时，加入 UnsupportedOperationException 未实现异常

    private JCheckBox autoExtendPluginCheckBox; // 若类继承插件，则创建时自动继承AbstractJUnitTestPlugIn

    private JCheckBox addDisplayNameCheckBox; //自动添加DisplayName注解
    private JCheckBox addNOClientMockCheckBox; //自动添加NOClientMock注解
    private JCheckBox addCustomParamsCheckBox; //自动添加CustomParams注解

    private JCheckBox autoGenJWYTestCheckBox; // 创建时，自动添加init、release方法，并自动引用core包（PLM 贾武阳）


    public String getImportPackages() {
        return importPackagesTextArea.getText();
    }

    public void setImportPackages(String importPackages) {
        importPackagesTextArea.setText(importPackages);
    }

    public boolean isShowConfirmSelected() {
        return showConfirmCheckBox.isSelected();
    }

    public void setShowConfirmSelected(boolean selected) {
        showConfirmCheckBox.setSelected(selected);
    }

    public boolean isGenPublicSelected() {
        return genPublicCheckBox.isSelected();
    }

    public void setGenPublicSelected(boolean selected) {
        genPublicCheckBox.setSelected(selected);
    }

    public boolean isGenProtectedSelected() {
        return genProtectedCheckBox.isSelected();
    }

    public void setGenProtectedSelected(boolean selected) {
        genProtectedCheckBox.setSelected(selected);
    }

    public boolean isGenPrivateSelected() {
        return genPrivateCheckBox.isSelected();
    }

    public void setGenPrivateSelected(boolean selected) {
        genPrivateCheckBox.setSelected(selected);
    }

    public boolean isAddUnsupportedExSelected() {
        return addUnsupportedExCheckBox.isSelected();
    }

    public void setAddUnsupportedExSelected(boolean selected) {
        addUnsupportedExCheckBox.setSelected(selected);
    }


    public boolean isAutoExtendPluginSelected() {
        return autoExtendPluginCheckBox.isSelected();
    }

    public void setAutoExtendPluginSelected(boolean selected) {
        autoExtendPluginCheckBox.setSelected(selected);
    }

    public boolean isAddDisplayNameSelected() {
        return addDisplayNameCheckBox.isSelected();
    }

    public void setAddDisplayNameSelected(boolean selected) {
        addDisplayNameCheckBox.setSelected(selected);
    }

    public boolean isAddNOClientMockSelected() {
        return addNOClientMockCheckBox.isSelected();
    }

    public void setAddNOClientMockSelected(boolean selected) {
        addNOClientMockCheckBox.setSelected(selected);
    }

    public boolean isAddCustomParamsSelected() {
        return addCustomParamsCheckBox.isSelected();
    }

    public void setAddCustomParamsSelected(boolean selected) {
        addCustomParamsCheckBox.setSelected(selected);
    }

    public boolean isAutoGenJWYTestSelected() {
        return autoGenJWYTestCheckBox.isSelected();
    }

    public void setAutoGenJWYTestSelected(boolean selected) {
        autoGenJWYTestCheckBox.setSelected(selected);
    }
    //endregion

    public PluginSettingsUI() {
//        JTextField option1Field = new JTextField(20);
//        option1Field.setToolTipText("Enter the value for Option 1"); // 设置提示信息

        // 创建主面板并设置 GridBagLayout，如果使用new GridBagLayout()，则无法布局到左上角
        mainPanel = new JPanel(null);

        GridBagConstraints gbc = new GridBagConstraints();

        // 设置 GridBagLayout 的默认对齐方式
        mainPanel.setAlignmentX(Component.LEFT_ALIGNMENT);  // 设置整体面板左对齐
        mainPanel.setAlignmentY(Component.TOP_ALIGNMENT);  // 设置整体面板顶部对齐

        // 通用设置：组件间距和对齐方式
        gbc.insets = new Insets(5, 5, 5, 5);  // 设置组件之间的间距
        gbc.anchor = GridBagConstraints.NORTHWEST;  // 设置组件左对齐
        gbc.fill = GridBagConstraints.NONE;  // 不让组件拉伸，保持原始大小

        showConfirmCheckBox = new JCheckBox("每次创建或更新时，弹窗确认单元测试类名");
        createUIComponents(0, 0, 2, gbc, showConfirmCheckBox);

        createUIComponents(1, 0, 2, gbc, createJSeparator());

        genPublicCheckBox = new JCheckBox("生成 public 测试方法");
        createUIComponents(2, 0, 2, gbc, genPublicCheckBox);

        genProtectedCheckBox = new JCheckBox("生成 protected 测试方法");
        createUIComponents(3, 0, 2, gbc, genProtectedCheckBox);

        genPrivateCheckBox = new JCheckBox("生成 private 测试方法");
        createUIComponents(4, 0, 2, gbc, genPrivateCheckBox);

        addUnsupportedExCheckBox = new JCheckBox("新增方式时，加入 UnsupportedOperationException 未实现异常");
        createUIComponents(5, 0, 2, gbc, addUnsupportedExCheckBox);

        createUIComponents(6, 0, 2, gbc, createJSeparator());

        addDisplayNameCheckBox = new JCheckBox("测试方法自动添加 DisplayName 注解");
        createUIComponents(7, 0, 2, gbc, addDisplayNameCheckBox);

        addNOClientMockCheckBox = new JCheckBox("测试方法自动添加 NOClientMock 注解");
        createUIComponents(8, 0, 2, gbc, addNOClientMockCheckBox);

        addCustomParamsCheckBox = new JCheckBox("测试方法自动添加 CustomParams 注解");
        createUIComponents(9, 0, 2, gbc, addCustomParamsCheckBox);

        createUIComponents(10, 0, 2, gbc, createJSeparator());

        autoExtendPluginCheckBox = new JCheckBox("若类继承插件，则创建时自动继承 AbstractJUnitTestPlugIn，并添加 @Form 注解");
        createUIComponents(11, 0, 2, gbc, autoExtendPluginCheckBox);

        autoGenJWYTestCheckBox = new JCheckBox("创建时，自动添加init、release方法，并自动引用core包（by PLM 贾武阳）");
        createUIComponents(12, 0, 2, gbc, autoGenJWYTestCheckBox);

        createUIComponents(13, 0, 2, gbc, createJSeparator());

        var label = new JLabel("创建时，引入的依赖包，必须以英文分号结尾");
        createUIComponents(14, 0, 2, gbc, label);

        importPackagesTextArea = new JTextArea();
        importPackagesTextArea.setLineWrap(true); // 自动换行
        importPackagesTextArea.setWrapStyleWord(true); // 单词边界换行
        JScrollPane scrollPane = new JScrollPane(importPackagesTextArea);
        createUIComponents(15, 0, 2, gbc, scrollPane);

        // 占位符，强制布局从左上角开始
        gbc.weighty = 1;  // 将剩余空间分配给占位符
        createUIComponents(16, 0, 0, gbc, Box.createVerticalGlue());
    }

    /**
     * description: 按布局填充组件
     * @author: feng_jun_peng
     * @create: 2024-12-18 10:25
     * @param row: 行
     * @param column: 列
     * @param columnWidth: 列跨度
     * @param gbc: 布局
     * @param component: 组件
     * @return: void
     */
    private void createUIComponents(int row, int column, int columnWidth, GridBagConstraints gbc, Component component) {
        /* 这种方法无法布局到左上角
        gbc.gridx = column;
        gbc.gridy = row;
        gbc.gridwidth = columnWidth;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        mainPanel.add(component, gbc);
         */
        var lineHeight = 30;
        if (component instanceof JSeparator) {
            lineHeight = 8;
        } else if (component instanceof JScrollPane) {
            lineHeight = 150;
        }

        component.setBounds(20, height, 600, lineHeight); // 设置位置和大小
        mainPanel.add(component);
        height += lineHeight;
    }

    private int height = 5;

    private JSeparator createJSeparator() {
        var separator1 = new JSeparator();
        separator1.setPreferredSize(new Dimension(400, 2));
        return separator1;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

}

